package com.mars.tukuan.web.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.base.BaseController;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.Paginator;
import com.mars.base.common.utils.date.DateUtil;
import com.mars.tukuan.dto.CreditMgrVo;
import com.mars.tukuan.rpc.api.CreditMgrService;

@RestController
@RequestMapping(value = "/api/tukuan/car/credit")
public class CreditMgrController extends BaseController {

	@Autowired
	private CreditMgrService CreditMgrService;

	@PostMapping(value = "/add")
	public Object add(@RequestBody CreditMgrVo entity) {
		try {
			if (StringUtils.isBlank(entity.getUsername())) {
				return BaseResult.failure("用户名不能为空");
			}
			if (StringUtils.isBlank(entity.getMobile())) {
				return BaseResult.failure("手机号码不能为空");
			}
			this.CreditMgrService.insertSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		try {
			this.CreditMgrService.delete(id);
			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@PostMapping(value = "/update")
	public Object update(@RequestBody CreditMgrVo entity) {
		try {
			if (StringUtils.isBlank(entity.getUsername())) {
				return BaseResult.failure("用户名不能为空");
			}
			if (StringUtils.isBlank(entity.getMobile())) {
				return BaseResult.failure("手机号码不能为空");
			}
			this.CreditMgrService.updateSelective(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}

	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.CreditMgrService.selectById(id);
	}

	@GetMapping(value = "/list")
	public Object list(CreditMgrVo entity) {
		return this.CreditMgrService.selectPage(new Paginator(this.request), entity);
	}
	
	
	@PostMapping(value = "/setDelay")
	public Object setDelay(@RequestBody CreditMgrVo entity) {
		try {
			Date addDay = DateUtil.addDay(entity.getMonitorDate(), entity.getDelay()) ;
			entity.setMonitorDate(addDay);
			
			this.CreditMgrService.setDelay(entity);

			return BaseResult.ok();
		} catch (AppException e) {
			throw e;
		}
	}
}
